package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.TbContentCategoryMapper;
import com.dongdongshop.model.TbContentCategory;
import com.dongdongshop.model.TbContentCategoryExample;
import com.dongdongshop.service.CategoryService;
import com.dongdongshop.vo.CategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private TbContentCategoryMapper contentCategoryMapper;

    @Override
    public List<CategoryVO> getAll() {
        List<TbContentCategory> categories = contentCategoryMapper.selectByExample(null);
        List<CategoryVO> categoryVOList = JSONObject.parseArray(JSONObject.toJSONString(categories),CategoryVO.class);
        return categoryVOList;
    }

    @Override
    public void addThis(CategoryVO vo) {
        TbContentCategory category = new TbContentCategory();
        BeanUtils.copyProperties(vo,category);
        contentCategoryMapper.insertSelective(category);
    }

    @Override
    public CategoryVO toUpdate(Long id) {
        TbContentCategory category = contentCategoryMapper.selectByPrimaryKey(id);
        CategoryVO vo = new CategoryVO();
        BeanUtils.copyProperties(category,vo);
        return vo;
    }

    @Override
    public void updateCategory(CategoryVO vo) {
        TbContentCategory category = new TbContentCategory();
        BeanUtils.copyProperties(vo,category);
        contentCategoryMapper.updateByPrimaryKeySelective(category);
    }

    @Override
    public int deleteBatch(Long[] ids) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        List<Long> list = Arrays.asList(ids);
        criteria.andIdIn(list);
        return contentCategoryMapper.deleteByExample(example);
    }
}
